import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app.module";
import { Logger, VersioningType } from "@nestjs/common";
import { CorsLib } from "src/libs/cors.lib";
import { SwaggerLib } from "src/libs/swagger.lib";
import { Commands } from "src/commands";
import { ConfigService } from "@nestjs/config";
import { PackageScript } from "./scripts/package.script";
import { PackageJson } from "tsconfig-paths/lib/filesystem";
import { RedisTool } from "./tools/redis.tool";

(async function () {
  const app = await NestFactory.create(AppModule);

  // 配置文件
  const configService = app.get(ConfigService);
  Logger.verbose("当前环境：" + JSON.stringify(configService.get("NestEnv")));

  // 开启版本控制
  app.enableVersioning({ type:VersioningType.URI });

  // 初始化redis连接
  // RedisTool.initConfig();

  // 解决跨域
  new CorsLib(app);

  // 全局接口前缀
  // app.setGlobalPrefix("webxue");

  // 构建swagger
  // new SwaggerLib({ app }).build();

  // 定时任务
  // new Commands(configService);

  await app.listen(configService.get("Port"), async () => {
    const PackageJson = (await PackageScript.getPackageJson()) as PackageJson;
    Logger.log("-----------------------------------");
    Logger.log(`${PackageJson.name} - 服务启动成功，访问地址：${configService.get("Host")}:${configService.get("Port")}`);
    Logger.log("-----------------------------------");
  });
})()
